package pikes.ecma;

public class VariableDeclaration extends PrintableSourceElement {

	private Identifier identifier = null;

	private IAssignmentExpression assignmentExpression = null;

	public VariableDeclaration(CharSequence identifier) {
		super();
		this.identifier = new Identifier(identifier);
	}

	public VariableDeclaration(CharSequence identifier, IAssignmentExpression assignmentExpression) {
		this(identifier);
		this.assignmentExpression = assignmentExpression;
	}

	public void visit(SyntaxTreeVisitor syntaxTreeVisitor) throws EcmaSyntaxVisitingException {
		identifier.visit(syntaxTreeVisitor);
		if (assignmentExpression != null) {
			syntaxTreeVisitor.equalSign();
			assignmentExpression.visit(syntaxTreeVisitor);
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof VariableDeclaration) {
			VariableDeclaration thatVarDecl = (VariableDeclaration) obj;
			if (!identifier.equals(thatVarDecl.identifier)) {
				return false;
			}

			if (assignmentExpression == null) {
				return thatVarDecl.assignmentExpression == null;
			}

			return assignmentExpression.equals(thatVarDecl.assignmentExpression);
		}
		return false;
	}

	@Override
	public int hashCode() {
		int hash = identifier.hashCode();
		if (assignmentExpression != null) {
			hash = hash + assignmentExpression.hashCode();
		}
		return hash;
	}

}
